﻿using System;
using System.Linq;
using ORM.Business.Feedbacks;
using ORM.Business.Interfaces;
using ORM.Business.Models.Entities;
using ORM.Common.Logging;
using ORM.Resources;

namespace ORM.Business.Repositories
{
    public class ApplicationRepository : IApplicationRepository
    {
        #region Implementation of IApplicationRepository

        public Feedback<int> IncreasePageView()
        {
            try
            {
                using (var entities = new OrmEntities())
                {
                    var exist = entities.VisitorNumbers.FirstOrDefault(i => i.Date == DateTime.Today);
                    if (exist != null)
                    {
                        exist.Visitors++;
                    }
                    else
                    {
                        exist = new VisitorNumber { Visitors = 1, Date = DateTime.Today };
                        entities.VisitorNumbers.Add(exist);
                    }

                    entities.SaveChanges();

                    return new Feedback<int>(true, null, exist.Visitors);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return new Feedback<int>(false, Messages.GeneralError);
            }
        }

        public StatisticsFeedback GetStatistics()
        {
            try
            {
                using (var entities = new OrmEntities())
                {
                    StatisticsFeedback feedback = new StatisticsFeedback(true, null);                 

                    feedback.AnswerNumber = (from c in entities.Answers
                                             join n in entities.Questions on c.QuestionId equals n.Id
                                             where !c.IsDeleted && !n.IsDeleted
                                             select new { c.Id }).Count();
                    
                    feedback.AllUserNumber = entities.Users.Count(i => !i.IsDeleted);
                    feedback.QuestionNumber = entities.Questions.Count(i => !i.IsDeleted);

                    return feedback;
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return new StatisticsFeedback(false, Messages.GeneralError);
            }
        }

        public ReportedStatisticsFeedback GetReportedStatistics()
        {
            try
            {
                using (var entities = new OrmEntities())
                {
                    ReportedStatisticsFeedback feedback = new ReportedStatisticsFeedback(true, null);


                    feedback.AnswerNumber = (from c in entities.Answers
                                             join n in entities.Questions on c.QuestionId equals n.Id
                                             where !c.IsDeleted && !n.IsDeleted && c.IsApproved
                                             select new { c.Id }).Count();
                    feedback.AllUserNumber = entities.Users.Count(i => !i.IsDeleted);
                    feedback.GuideNumber = entities.Guides.Count(i => !i.IsDeleted && i.isApproved);
                    feedback.QuestionNumber = entities.Questions.Count(i => !i.IsDeleted);
                    feedback.CommentNumber = (from c in entities.GuideComments
                                             join n in entities.Guides on c.GuideId equals n.Id
                                             where !c.IsDeleted && !n.IsDeleted && n.isApproved
                                             select new { c.Id }).Count();

                    feedback.VisitorNumber = entities.VisitorNumbers.Sum(i => (int?)i.Visitors) ?? 0;
                    var today = entities.VisitorNumbers.First(i => i.Date == DateTime.Today);
                    feedback.VisitorsToday = today != null ? today.Visitors : 0;
                    return feedback;
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                return new ReportedStatisticsFeedback(false, Messages.GeneralError);
            }
        }

        #endregion
    }
}